package com.promise.jokerdream.ui.components

import androidx.compose.foundation.layout.*
import androidx.compose.material3.*
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.unit.dp
import com.promise.jokerdream.ui.theme.AppFontSizes

/**
 * 任务名字输入对话框
 */
@Composable
fun TaskNameInputDialog(
    show: Boolean,
    initialText: String = "",
    onTaskNameEntered: (String) -> Unit,
    onDismiss: () -> Unit,
    modifier: Modifier = Modifier
) {
    var taskName by remember { mutableStateOf(initialText) }
    var isValid by remember { mutableStateOf(true) }
    var triggerValidation by remember { mutableStateOf(0) }

    // 状态重置函数
    fun resetState() {
        taskName = initialText
        isValid = true
        triggerValidation = 0
    }
    
    // 每次显示时重置状态
    LaunchedEffect(show) {
        if (show) {
            taskName = initialText
            isValid = true
            triggerValidation = 0
        }
    }

    PMDialog(
        show = show,
        title = if (initialText.isEmpty()) "新建任务" else "编辑任务",
        confirmText = "确定",
        cancelText = "取消",
        onDismiss = onDismiss,
        onConfirm = {
            triggerValidation++
            if (isValid && taskName.isNotBlank()) {
                onTaskNameEntered(taskName.trim())
            }
        }
    ) {
        OutlinedTextField(
            value = taskName,
            onValueChange = { 
                taskName = it
                isValid = it.isNotBlank()
            },
            placeholder = { Text("请输入任务名称") },
            singleLine = true,
            isError = triggerValidation > 0 && !isValid,
            modifier = Modifier.fillMaxWidth()
        )
    }
}
